using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Windows;
using System.Windows.Forms;
using System.Windows.Markup;
using Common;
using SSettings;

namespace MainView.Windows
{
    public partial class ExportExperimentDataWindow : Window, IComponentConnector
    {
        public static readonly DependencyProperty ExportDataListProperty = DependencyProperty.Register("ExportDataList", typeof(List<ExportData>), typeof(ExportExperimentDataWindow), new UIPropertyMetadata(null));

        public static readonly DependencyProperty PathProperty = DependencyProperty.Register("Path", typeof(string), typeof(ExportExperimentDataWindow), new UIPropertyMetadata(null));

        public static readonly DependencyProperty FileNameProperty = DependencyProperty.Register("FileName", typeof(string), typeof(ExportExperimentDataWindow), new UIPropertyMetadata(null));

        public static readonly DependencyProperty FileExtProperty = DependencyProperty.Register("FileExt", typeof(string), typeof(ExportExperimentDataWindow), new UIPropertyMetadata(null));

        public List<ExportData> ExportDataList
        {
            get
            {
                return (List<ExportData>)GetValue(ExportDataListProperty);
            }
            set
            {
                SetValue(ExportDataListProperty, value);
            }
        }

        public string Path
        {
            get
            {
                return (string)GetValue(PathProperty);
            }
            set
            {
                SetValue(PathProperty, value);
            }
        }

        public string FileName
        {
            get
            {
                return (string)GetValue(FileNameProperty);
            }
            set
            {
                SetValue(FileNameProperty, value);
            }
        }

        public string FileExt
        {
            get
            {
                return (string)GetValue(FileExtProperty);
            }
            set
            {
                SetValue(FileExtProperty, value);
            }
        }

        public ExportExperimentDataWindow()
        {
            Path = ConfigSettings.GetInstance().MemorySettings.ExperimentExportPath;
            FileExt = ConfigSettings.GetInstance().MemorySettings.ExperimentExportExt;
            InitializeComponent();
            comboBoxExt.ItemsSource = new List<string> { "xlsx", "csv", "txt" };
        }

        public string GetFilePath()
        {
            return System.IO.Path.GetFullPath(Path) + "\\" + FileName + "." + FileExt;
        }

        private void btnOK_Click(object sender, RoutedEventArgs e)
        {
            if (ExportDataList.Where((ExportData s) => s.IsExport).Count() == 0)
            {
                System.Windows.MessageBox.Show(Utility.GetMessage("msg_sellected_data_empty"));
            }
            else if (!Directory.Exists(Path))
            {
                System.Windows.MessageBox.Show(Utility.GetMessage("msg_folder_not_exist"));
                textBoxPath.Focus();
                textBoxPath.SelectAll();
            }
            else if (string.IsNullOrEmpty(FileName))
            {
                System.Windows.MessageBox.Show(Utility.GetMessage("msg_filename_is_null"));
                textBoxFileName.Focus();
                textBoxFileName.SelectAll();
            }
            else if (File.Exists(GetFilePath()) && System.Windows.MessageBox.Show(Utility.GetMessage("msg_file_exist_cover"), Utility.GetMessage("Warning"), MessageBoxButton.YesNo, MessageBoxImage.Exclamation, MessageBoxResult.No) == MessageBoxResult.No)
            {
                textBoxFileName.Focus();
                textBoxFileName.SelectAll();
            }
            else
            {
                ConfigSettings.GetInstance().MemorySettings.ExperimentExportPath = Path;
                ConfigSettings.GetInstance().MemorySettings.ExperimentExportExt = FileExt;
                base.DialogResult = true;
                Close();
            }
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            Close();
        }

        private void btnBrowser_Click(object sender, RoutedEventArgs e)
        {
            FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog();
            folderBrowserDialog.RootFolder = Environment.SpecialFolder.Desktop;
            folderBrowserDialog.SelectedPath = Path;
            folderBrowserDialog.ShowNewFolderButton = true;
            if (folderBrowserDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                Path = folderBrowserDialog.SelectedPath;
            }
        }
    }
}
